<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<script th:inline="javascript">
    $(function () {
        /*<![CDATA[*/
        var myColumns = columns();
        battcn.ns('battcn.admin.menu[[${OP.menuId}]]');
        $('#admin_menu[[${OP.menuId}]]_datagrid').bootstrapTable({
            url: [[${MENU.channel}]] + '/query',
            height: '100%',
            sortName: 'updateDate',
            sortOrder: 'desc',
            striped: true,
            pagination: true,
            pageSize: 10,
            pageList: [10, 25, 50, 100, 200],
            search: false,
            sidePagination: 'server',
            idField: 'id',
            uniqueId: 'id',
            responseHandler: responseHandler,
            queryParams: queryParams,
            minimumCountColumns: 2,
            clickToSelect: true,
            columns: myColumns

        });

        //导出日志
        battcn.admin.menu[[${OP.menuId}]].export = function () {
            battcn.confirm(function () {
                location.href = "/op_export_[[${OP.menuId}]]";
            });
        };

        // 传递的参数
        function queryParams(params) {
            return {
                pageSize: params.limit,
                pageNum: params.offset / params.limit + 1,
                sort: params.sort,
                order: params.order,
                status: $("#status3").val()
            }
        }

        //查询
        var search = 0;
        battcn.admin.menu[[${OP.menuId}]].search = function () {
            var myColumns = columns();
            if ($("#status3").val() !== '') {
                $('#admin_menu[[${OP.menuId}]]_datagrid').bootstrapTable("refreshOptions",
                    {
                        columns : myColumns,
                    }  );
                search++;
            } else {
                if (search !== 0) {
                    $('#admin_menu[[${OP.menuId}]]_datagrid').bootstrapTable("refreshOptions",
                        {
                            columns : myColumns,
                        }  );
                    search = 0;
                }
            }
        };

        //出金
        battcn.admin.menu[[${OP.menuId}]].updateStatus = function () {
            var rows =$('#admin_menu[[${OP.menuId}]]_datagrid').bootstrapTable('getSelections');
            if(rows.length == 0){
                battcn.toastrsAlert({
                    code:'info',
                    msg:'请选择你要复核的记录'
                });
                return;
            }

            if(rows[0].status != "3"){
                layer.msg('不是待出金的数据，无法出金');
                return;
            }
            $.get('/op_edit_[[${OP.menuId}]]?expid='+rows[0].id, {}, function (str) {
                var index = layer.open({
                    type: 1,
                    title:'出金',
                    btn: ['通过', '驳回'],
                    shadeClose: false,
                    moveOut: true,
                    shade: 0.5,
                    maxmin: true,
                    area: ['50%', '70%'],
                    content: str,
                    yes: function(){
                        battcn.admin.menu[[${OP.menuId}]].save("4");
                    },
                    btn2: function(){
                        battcn.admin.menu[[${OP.menuId}]].save("5");
                    }
                });
                initUI();
            });
        };

        //日期时间范围
//        laydate.render({
//            elem: '#datetime'
//            , type: 'datetime'
//            , range: true
//        });
        /*]]>*/

    });

    //新增
    battcn.admin.menu[[${OP.menuId}]].add = function(){
        battcn.showWindow({
            title:'添加经费申请',
            href:'/op_edit_[[${OP.menuId}]]',
            width:'50%',
            height:'65%',
            okhandler:function(){
                battcn.admin.menu[[${OP.menuId}]].save();
            },
            cancelhandler:function(){
                battcn.closeWindow();
            }
        });

    }

    //编辑
    battcn.admin.menu[[${OP.menuId}]].edit = function(){
        var rows = $('#admin_menu[[${OP.menuId}]]_datagrid').bootstrapTable('getSelections');
        if(rows.length == 0){
            battcn.toastrsAlert({
                code:'info',
                msg:'请选择你要编辑的记录'
            });
            return;
        }
        if(rows.length > 1){
            battcn.toastrsAlert({
                code:'warning',
                msg:'sorry , 只能选择一条进行编辑'
            });
            return;
        }
        //console.log(rows[0]);

        if(rows[0].status != "0"){
            layer.confirm('修改之后状态会变成未审核状态', {icon: 3, title:'提示'},function (index) {
                _edit();
                layer.close(index);
            });
        }else{
            _edit();
        }
    };
    function _edit(){
        var rows = $('#admin_menu[[${OP.menuId}]]_datagrid').bootstrapTable('getSelections');
        battcn.showWindow({
            title:'编辑经费申请',
            href: '/op_edit_[[${OP.menuId}]]?id='+rows[0].id,
            width:'50%',
            height:'60%',
            okhandler:function(){
                battcn.admin.menu[[${OP.menuId}]].save();
            },
            cancelhandler:function(){
                battcn.closeWindow();
            }
        });
    }


    //删除
    battcn.admin.menu[[${OP.menuId}]].remove = function(){
        var rows =$('#admin_menu[[${OP.menuId}]]_datagrid').bootstrapTable('getSelections');
        if(rows.length == 0){
            battcn.toastrsAlert({
                code:'info',
                msg:'请选择你要删除的记录'
            });
            return;
        }

        var flag = true;
        $.each(rows,function(i,n){
            if(n.status == "3"){
                layer.msg('其中已出金的数据，无法删除');
            }
        });

        battcn.confirm(function(){
            var ps = [];
            $.each(rows,function(i,n){
                if(n.status != "3"){
                    ps.push(n.id);
                }
            });
            battcn.ajaxJson({url:'/op_remove_[[${OP.menuId}]]',data:{"ids":ps.join(",")}},function(){


                $('#admin_menu[[${OP.menuId}]]_datagrid').bootstrapTable("refreshOptions",
                    {
                        columns : columns()
                    }  );

            });
        });
    }

    function columns(){
        var status = "复核";
        if($("#status3").val() == "2"){
            status = "复核";
        }else if($("#status3").val() == "3"){
            status = "出金";
        }
        return [{
            checkbox: true
        }, {
            field: 'content',
            title: '经费事由',
            align: 'center',
            valign: 'middle'
        }, {
            field: 'money',
            title: '金额',
            align: 'center',
            sortable: true,
            valign: 'middle'
        }, {
            field: 'happenDate',
            title: '发生时间',
            align: 'center',
            valign: 'middle',
            sortable: true,
            formatter:function(value,row,index){return new Date(value).pattern("yyyy-MM-dd")}
        }, {
            field: 'createName',
            title: '申请人',
            align: 'center',
            valign: 'middle'
        }, {
            field: 'status',
            title: '状态',
            align: 'center',
            valign: 'middle',
            formatter: function(value,row,index){
                if(value == '0'){
                    return '<span style="color: orangered">未审批</span>';
                }else if(value == '1'){
                    return '<span style="color: orangered">部门已审批</span>';
                }else if(value == '2'){
                    return '<span style="color: orangered">会计已复核</span>';
                }else if(value == '3'){
                    return '<span style="color: orangered">经理已复核</span>';
                }else if(value == '4'){
                    return '<span style="color: green">已出金</span>';
                }else if(value == '5'){
                    return '<span style="color: red">被驳回</span>';
                }
                //return value == 1 ? '<i class="fa fa-check text-navy"></i>' : '<i class="fa fa-close text-danger"></i>'
            }
        }, {
            field: 'createName',
            title: status+'人',
            align: 'center',
            sortable: true,
            valign: 'middle'
        }, {
            field: 'createTime',
            title: status+'时间',
            align: 'center',
            valign: 'middle',
            sortable: true,
            formatter:function(value,row,index){return new Date(value).pattern("yyyy-MM-dd HH:mm:ss")}
        }]
    }
</script>
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="ibox float-e-margins">
        <div class="ibox-content">
            <div class="panel panel-default">
                <div class="panel-body">
                    <form id="formSearch" class="form-horizontal">
                        <div class="form-group" style="margin-top: 15px">
                            <label class="control-label col-sm-1">状态</label>
                            <div class="col-sm-3">
                                <select id="status3" class="form-control">
                                    <option value="2">未复核</option>
                                    <option value="3">已复核</option>
                                </select>
                                <!--<input type="text" class="form-control" id="datetime"/>-->
                            </div>
                            <div class="col-sm-4" style="text-align: left;">
                                <button type="button"
                                        th:onclick="@{battcn.admin.menu{menuId}.search()(menuId=${OP.menuId})}"
                                        class="btn btn-primary">查询
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <div th:include="toolbar :: toolbar-operate"></div>
            <div class="table-responsive">
                <table th:id="@{admin_menu{menuId}_datagrid(menuId=${OP.menuId})}"
                       data-toolbar="#toolbar" data-show-refresh="true"
                       data-show-toggle="true" data-show-columns="true"
                       data-show-footer="false" data-mobile-responsive="true">
                </table>
            </div>
        </div>
    </div>
</div>
</html>